﻿Public Class Video
    Inherits File

    Dim Thumb As String
    Dim ThumbImage As Image

    Overrides Function Text() As String
        Dim S As String = MyBase.Text
        S &= vbTab & "thumb=" & Thumb
        Return S
    End Function

    Public Overrides Sub SaveXML(ByVal Doc As System.Xml.XmlTextWriter)
        Doc.WriteAttributeString("thumb", Thumb)
    End Sub

    Public Overrides Sub SaveDB()
        ExecSQL("update items set thumb = @t where id = @id", Thumb, ID)
    End Sub

    Public Overrides Sub Parse(ByVal Item As String)
        Thumb = Item.Parse("thumb")
    End Sub

    Sub Open()
        System.Diagnostics.Process.Start(MainFolder & Title)
    End Sub

    Overrides Sub Draw(ByVal G As Graphics, ByVal R As Rectangle, ByVal IsSelected As Boolean, ByVal IsHot As Boolean, ByVal IsFocused As Boolean, ByVal IsDropTarget As Boolean)
        DrawFocusRect(G, R, IsSelected, IsHot, IsDropTarget, IsFocused, Item.ListView.Focused)
        R.Inflate(-R.Width \ 24, -R.Height \ 24)
        R.Inflate(-5, -5)
        Dim Thumb As Image = Nothing
        If Me.Thumb = "" Then
            Thumb = My.Resources.video
        Else
            If ThumbImage Is Nothing Then
                ThumbImage = New Bitmap(MainFolder & Me.Thumb)
                Dim TR As New Rectangle(ThumbImage.Width \ 2, ThumbImage.Height \ 2, 0, 0)
                TR.Inflate(ThumbImage.Height \ 6, ThumbImage.Height \ 6)
                Dim TG As Graphics = Graphics.FromImage(ThumbImage)
                TG.DrawImageWithOpacity(My.Resources.playvideo, TR, 0.6)
            End If
            Thumb = ThumbImage
        End If
        If Thumb.Width / R.Width * R.Height > Thumb.Height Then R.Inflate(0, -(R.Height - R.Width / Thumb.Width * Thumb.Height) / 2) Else R.Inflate(-(R.Width - R.Height / Thumb.Height * Thumb.Width) / 2, 0)
        R.Inflate(5, 5)
        Dim BitmapName As String = Title & R.Width
        If Not BitmapManager.Exists(BitmapName) Then
            Dim z As Rectangle = R
            Dim B As New Bitmap(R.Width, R.Height)
            R.Offset(-R.Left, -R.Top)
            'R.Inflate(-2, -2)
            Dim Gr As Graphics = Graphics.FromImage(B)
            Gr.InterpolationMode = Drawing2D.InterpolationMode.High
            Gr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            Gr.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
            Gr.DrawShadow(R)
            R.Inflate(-5, -5)
            'R.Offset(-2, -2)
            Gr.DrawImage(Thumb, R)
            BitmapManager.Add(BitmapName, B)
            Gr.Dispose()
            R = z
        End If
        DrawImageWithOpacity(G, BitmapManager.GetBitmap(BitmapName), R, If(IsCut, 0.4, 1))
    End Sub

    Sub SetThumb(ByVal ThumbName As String)
        ThumbImage = Nothing
        Thumb = Title & ".jpg"
        Dim Image As New Bitmap(ThumbName)
        Image.Save(MainFolder & Thumb, System.Drawing.Imaging.ImageFormat.Jpeg)
		Redraw()
    End Sub

End Class
